/*
* @Author: yyf
* @Date: 2018/4/10
* @description: 投资事件 添加页 标的企业财务
*/
<template>
    <div>
        <el-row class="custom-title m-t-20 m-b-20">
            <h3>标的企业财务
                <span class="m-l-20">最后更新时间：2017-1-1 01:00</span>
                <el-button type="primary" class="m-l-20" @click="saveFinance" round>保存</el-button>
            </h3>
        </el-row>
        <el-form label-width="90px" ref="financeForm" :rules="financeRules" :model="financeInEventDto">
            <div class="custom-box p-20">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="起始时间" prop="startTime">
                            <el-date-picker class="w-100" v-model="financeInEventDto.startTime" type="date" placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="1">
                        <el-form-item label="截止时间" prop="endTime">
                            <el-date-picker class="w-100" v-model="financeInEventDto.endTime" type="date" placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label-width="10px">
                            <el-button type="primary" @click="getCompanyFinance">调取</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="报表类型">
                            <el-select disabled v-model="comFinanceVO.reportType" class="w-100">
                                <el-option 
                                    v-for="item in financeReportType" 
                                    :key="item.dictValue" 
                                    :value="item.dictValue-0" 
                                    :label="item.dictName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="1">
                        <el-form-item label="币种" label-width="90px">
                            <el-select disabled v-model="comFinanceVO.currencyType" class="w-100">
                                <el-option 
                                    v-for="item in currencyType" 
                                    :key="item.dictValue" 
                                    :value="item.dictValue-0" 
                                    :label="item.dictName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="总资产">
                            <el-input disabled v-model="comFinanceVO.assets">
                                <template slot="append">万</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="营业收入">
                            <el-input disabled v-model="comFinanceVO.businessIncome">
                                <template slot="append">万</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="营业利润">
                            <el-input disabled v-model="comFinanceVO.operatingProfit">
                                <template slot="append">万</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="净利润">
                            <el-input disabled v-model="comFinanceVO.netProfit">
                                <template slot="append">万</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="1">
                        <el-form-item label="净资产">
                            <el-input disabled v-model="comFinanceVO.netAssets">
                                <template slot="append">万</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="归属于母公司净资产" class="line-h-20">
                            <el-input disabled v-model="comFinanceVO.parentAsset">
                                <template slot="append">万</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="利润总额">
                            <el-input disabled v-model="comFinanceVO.totalProfit">
                                <template slot="append">万</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="归属于母公司净利润" class="line-h-20">
                            <el-input disabled v-model="comFinanceVO.parentNetProfit">
                                <template slot="append">万</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
        </el-form>
        <el-form label-width="100px">
            <div class="custom-box p-20 m-t-20">
                <el-row class="custom-title">
                    <h3 class="m-r-20">比率</h3>
                    <el-button size="medium" type="primary" @click="calculateRate">计算</el-button>
                </el-row>
                <el-row class="m-t-20">
                    <el-col :span="6">
                        <el-form-item label="市销率">
                            <el-input v-model="eventFinanceDto.marketRate"></el-input>
                        </el-form-item>
                        <el-form-item label="市净率">
                            <el-input v-model="eventFinanceDto.netRateAfter"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="市盈率">
                            <el-input v-model="eventFinanceDto.peRateAfter"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="备注">
                            <el-input type="textarea" v-model="eventFinanceDto.remark" :rows="4"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row class="custom-title m-t-20">
                    <h3>审计公司</h3>
                    <el-table :data="comFinanceData">
                        <el-table-column label="报告ID" prop="id"></el-table-column>
                        <el-table-column label="报告名称" prop="reportName"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="text" @click="downloadReport(scope.row.reportName, scope.row.reportUrl)">下载</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-row>
            </div>
        </el-form>
        <el-dialog :visible.sync="financeDialog">
            <el-table :data="companyFinanceData">
                <el-table-column label="ID" prop="id"></el-table-column>
                <el-table-column label="财务年份" prop="startTime">
                    <template slot-scope="scope">
                        {{scope.row.startTime}}至{{scope.row.endTime}}
                    </template>
                </el-table-column>
                <el-table-column label="财报类型" prop="financeType"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="addFinaceInfo(scope.row)">添加</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>
<script lang="ts">
    import Vue from 'vue';
    import { Component } from 'vue-property-decorator';
    import { EventFinanceDto, ComFinanceVO, EventDto, ComFinanceAuditVO } from '../../../../api/Interface';
    import { FinanceInEventDto, FinanceRateInEventDto } from '../../../../service/event/Interface';
    import EventService from '../../../../service/event/index';
    import FinanceService from '../../../../service/subject/company/finance-info/index';
    import EventFinanceService from '../../../../service/event/event-finance-controller/index';
    import getUrl from '../../../../service/getUrl';
    import Api from '../../../../api/common/commonController.api';
    @Component({
        components: {}
    })
    export default class EnterpriseFinance extends Vue {
        // 初始化数据 相当于data
        public financeReportType = this.$store.state.user.dictionary.dicList.financeReportType; // 报表类型
        public currencyType = this.$store.state.user.dictionary.dicList.currencyType; // 货币类型
        public eventFinanceDto: EventFinanceDto = {} as EventFinanceDto; // 添加标的企业财务参数
        public financeInEventDto: FinanceInEventDto = {} as FinanceInEventDto; // 根据时间调取财务信息
        public financeRateInEventDto: FinanceRateInEventDto = {} as FinanceRateInEventDto; // 根据事件id、财务Id获取企业财务信息比率信息
        public comFinanceVO: ComFinanceVO = {} as ComFinanceVO; // 自动添加的财务信息
        public companyFinanceData: Array<ComFinanceVO> = []; // 企业财务信息
        // 事件基本信息类型
        public eventBasicInfo: EventDto = {} as EventDto;
        // public comFinanceAuditVO: ComFinanceAuditVO = {} as ComFinanceAuditVO; // 审计公司
        public comFinanceData: Array<ComFinanceAuditVO> = []; // 审计公司
        public eventId: string = ''; // 事件ID
        public companyId: string = ''; // 企业ID
        public id: string = ''; // 标的企业财务ID（主id）
        public financeId: string = ''; // 财务ID
        public financeDialog: boolean = false;
        public financeRules = {
            startTime: [
                { required: true, message: '请选择开始时间', trigger: 'blur' },
            ],
            endTime: [
                { required: true, message: '请选择结束时间', trigger: 'blur' },
            ]
        };

        // refs
        public $refs: {
            financeForm: HTMLFormElement,
        };

        // watch
        // @Watch('msg')
        // public onChildChanged(val: string, oldVal: string) {
        //     return val + '***';
        // }

        // 声明周期钩子
        public mounted() {
            this.eventId = this.$route.params.eventId;
            if (this.eventId !== '-1') {
                this.getBasicInfo(); // 获取基本信息
                this.getEventFinanceInfo(); // 获取财务信息
            }
        }
        // 获取基本信息
        public async getBasicInfo() {
            const res = await EventService.getEventDetail(this.eventId);
            if (res.resCode === '0') {
                this.companyId = res.data.companyId;
                this.eventBasicInfo = res.data; // 企业基本信息
            }
        }
        // 根据事件id获取财务信息
        public async getEventFinanceInfo() {
            const res = await EventFinanceService.getEventFinanceInfo(this.eventId);
            if (res.resCode === '0') {
                this.id = res.data.id;
                this.financeId = res.data.financeId;
                this.eventFinanceDto = res.data;
                if (this.financeId) {
                    this.getComFinanceById();
                }
            }
        }
        // 根据财务id查询财务信息
        public async getComFinanceById() {
            const res = await FinanceService.getComFinanceById(Number(this.financeId));
            if (res.resCode === '0') {
                this.comFinanceVO = res.data;
                this.comFinanceData = res.data.comFinanceAuditList;
            }
        }
        // 调取财务信息
        public async getCompanyFinance() {
            this.$refs.financeForm.validate(async (valid: boolean) => {
                if (valid) {
                    this.financeDialog = true;
                    this.financeInEventDto.startTime = new Date(this.financeInEventDto.startTime).toString();
                    this.financeInEventDto.endTime = new Date(this.financeInEventDto.endTime).toString();
                    this.financeInEventDto.companyId = this.companyId;
                    const res = await EventFinanceService.getCompanyFinanceByTime(this.financeInEventDto);
                    if (res.resCode === '0') {
                        this.companyFinanceData = res.data; // 财务信息
                        this.comFinanceData = res.data.comFinanceAuditList; // 审计公司
                    }
                }
            });
        }
        // 添加财务比率
        public async addFinaceInfo(comFinanceVO: ComFinanceVO) {
            this.financeId = comFinanceVO.id.toString();
            this.eventFinanceDto.financeId = comFinanceVO.id;
            this.comFinanceVO = comFinanceVO;
            this.financeDialog = false;
            this.comFinanceData = comFinanceVO.comFinanceAuditList;
        }
        // 计算
        public async calculateRate() {
            this.financeRateInEventDto.eventId = this.eventId; 
            this.financeRateInEventDto.financeId = this.financeId; 
            if (!this.financeId) {
                this.$message.warning('请选择标的财务！');
            } else {
                const res = await EventFinanceService.getFinanceByFinanceId(this.financeRateInEventDto);
                if (res.resCode === '0') {
                    this.eventFinanceDto = res.data;
                }
            }
        }
        // 保存
        public saveFinance() {
            if ( !this.id ) {
                this.saveEventFinanceInfo();
            } else {
                this.updateEventFinance();
            }
        }
        // 添加标的企业财务
        public async saveEventFinanceInfo() {
            const res = await EventFinanceService.saveEventFinanceInfo(this.eventFinanceDto);
            if (res.resCode === '0') {
                this.$message.success('添加成功！');
            }
        }
        // 修改标的企业财务
        public async updateEventFinance() {
            const res = await EventFinanceService.updateEventFinance(this.eventFinanceDto);
            if (res.resCode === '0') {
                this.$message.success('修改成功！');
            }
        }
        // 下载审计报告
        public downloadReport(reportName: string, reportUrl: string) {
            if (reportUrl) {
                window.open(getUrl() + Api.download.url
                + '?Authorization=Bearer ' + this.$store.state.user.token
                + '&fileName=' + reportName
                + '&fileUrl=' + reportUrl);
            } else {
                this.$message.warning('下载地址有误！');
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    .noPass {
        color: #f00;
    }
    .w-100 {
        width: 100%;
    }
    .align-center-self {
        text-align: center;
        line-height: 40px;
    }
</style>